package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.SkuImagesEntity;
import com.atguigu.gulimall.product.entity.SpuInfoDescEntity;
import com.atguigu.gulimall.product.service.*;
import com.atguigu.gulimall.product.vo.ItemSaleAttrVo;
import com.atguigu.gulimall.product.vo.SkuItemVO;
import com.atguigu.gulimall.product.vo.SpuItemAttrGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.SkuInfoDao;
import com.atguigu.gulimall.product.entity.SkuInfoEntity;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuImagesService skuImagesService;

    @Autowired
    SpuInfoDescService spuInfoDescService;

    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    ExecutorService executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }
//
//    @Override
//    public SkuItemVO item(Long skuId) {
//        SkuItemVO skuItemVO = new SkuItemVO();
//        //1. sku的基本数据 pms_sku_info
//        SkuInfoEntity skuInfoEntity = baseMapper.selectById(skuId);
//        skuItemVO.setInfo(skuInfoEntity);
//        Long spuId = skuInfoEntity.getSpuId();
//        //2. sku的图片信息 pms_sku_images
//        List<SkuImagesEntity> skuImagesEntities = skuImagesService.list(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId));
//        skuItemVO.setImages(skuImagesEntities);
//        //3. spu的销售属性组合
//        List<ItemSaleAttrVo> salAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(spuId);
//        skuItemVO.setSaleAttr(salAttrVos);
//        //4. spu的介绍 spu_info_desc
//        SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(spuId);
//        skuItemVO.setDesc(spuInfoDescEntity);
//        //5. 获取的spu的规格参数信息
//        Long catalogId = skuInfoEntity.getCatalogId();
//        List<SpuItemAttrGroupVO> attrGroupVOS = attrGroupService.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
//        skuItemVO.setGroupAttrs(attrGroupVOS);
//
//        return skuItemVO;
//    }

    @Override
    public SkuItemVO item(Long skuId) {
        SkuItemVO skuItemVO = new SkuItemVO();
        //1. sku的基本数据 pms_sku_info
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfoEntity = baseMapper.selectById(skuId);
            skuItemVO.setInfo(skuInfoEntity);
            return skuInfoEntity;
        }, executor);

        //3. spu的销售属性组合
        CompletableFuture<Void> saleFuture = infoFuture.thenAcceptAsync((res) -> {
            List<ItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());
            skuItemVO.setSaleAttr(saleAttrVos);
        }, executor);

        //4. spu的介绍 spu_info_desc
        CompletableFuture<Void> descInfoFuture = infoFuture.thenAcceptAsync((res) -> {
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId());
            skuItemVO.setDesc(spuInfoDescEntity);
        }, executor);

        //5. 获取的spu的规格参数信息
        CompletableFuture<Void> attrFuture = infoFuture.thenAcceptAsync((res) -> {
            List<SpuItemAttrGroupVO> attrGroupVOS = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVO.setGroupAttrs(attrGroupVOS);
        }, executor);

        //2. sku的图片信息 pms_sku_images
        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> skuImagesEntities = skuImagesService.list(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId));
            skuItemVO.setImages(skuImagesEntities);
        }, executor);

        //阻塞，等全部完成后返回
        try {
            CompletableFuture.allOf(saleFuture,descInfoFuture,attrFuture,imageFuture).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return skuItemVO;
    }

}